package com.bsj.media.media.h26X;



import com.bsj.media.media.gb28181.decodeSPS;
import com.bsj.power.common.def.protocol.rtp.RtpParameterSet;

import java.util.Objects;

/**
 * @Description h264片段头  https://zhuanlan.zhihu.com/p/629305860
 * @Date 2023/10/27 9:55
 **/
public class H264SliceHead {

    /**
     * 获取当前slice所属的帧的帧号
     * @param frame: 去除帧头（00 00 00 01）的视频数据
     * @param spsInfo:
     * @return
     **/
    public static int getFrameNum(byte[] frame, H264NaluSPSInfo spsInfo) {
        if (Objects.isNull(spsInfo)) {
            return -1;
        }
        int[] startBit = new int[1];
        decodeSPS.de_emulation_prevention(frame, new int[]{frame.length});
        int forbidden_zero_bit = decodeSPS.u(1, frame, startBit);
        int nal_ref_idc = decodeSPS.u(2, frame, startBit);
        int nal_unit_type = decodeSPS.u(5, frame, startBit); // NALU单元的类型
        if (nal_unit_type == RtpParameterSet.naluTypeIDRH4 || nal_unit_type == RtpParameterSet.naluTypeNotIDRH4) {
            int first_mb_in_slice = decodeSPS.Ue(frame, frame.length, startBit);
            int slice_type = decodeSPS.Ue(frame, frame.length, startBit);
            int pic_parameter_set_id = decodeSPS.Ue(frame, frame.length, startBit);
            int frame_num = decodeSPS.u(spsInfo.log2_max_frame_num_minus4 + 4, frame, startBit);
            return frame_num;
        }
        return -1;
    }
}
